//
//  CommentCell.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/20.
//  Copyright © 2017年 tml. All rights reserved.
//

import UIKit

class CommentCell: UITableViewCell {

    fileprivate lazy var iconImgV: UIImageView = {
       let imgV = UIImageView()
        imgV.contentMode = .scaleAspectFill
        imgV.layer.cornerRadius = 20
        imgV.layer.masksToBounds = true
        return imgV
    }()
    
    fileprivate lazy var nameLB: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.font = UIFont.systemFont(ofSize: 13)
        return label
    }()
    
    fileprivate lazy var msgLable: MYCoreTextLabel = {
       let label = MYCoreTextLabel()
        label.textFont = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.black
        label.showWebLink = true
        label.webLinkFont = UIFont.systemFont(ofSize: 13)
        label.webLinkColor = kRGBA(r: 127, g: 221, b: 146, a: 1)
        label.webLinkBackColor = UIColor.clear
        //
        label.showMailLink = true
        label.mailLinkFont = UIFont.systemFont(ofSize: 13)
        label.mailLinkColor = kRGBA(r: 127, g: 221, b: 146, a: 1)
        label.mailLinkBackColor = UIColor.clear
        //
        label.showPhoneLink = true
        label.mailLinkFont = UIFont.systemFont(ofSize: 13)
        label.mailLinkColor = kRGBA(r: 127, g: 221, b: 146, a: 1)
        label.mailLinkBackColor = UIColor.clear
        label.delegate = self
        label.lineSpacing = 2.0
        label.wordSpacing = 0.5
        return label
    }()
    
    fileprivate func setUpSnap() {
        self.iconImgV.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().offset(10)
            make.width.height.equalTo(40)
        }
        
        self.nameLB.snp.makeConstraints { (make) in
            make.left.equalTo(self.iconImgV.snp.right).offset(10)
            make.top.equalTo(self.iconImgV)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(15)
        }
        

    }
    
    var model: CommentModel? {
        didSet{
            guard let model = model else {
                return
            }
            iconImgV.kf.setImage(urlString: model.face)
            nameLB.text = model.nickname
            self.msgLable.setText(model.content_filter, customLinks: nil, keywords: nil)
            let msgHeight = stringHeight(model.content_filter ?? "", 13, self.width - 70)
            self.msgLable.size = self.msgLable.sizeThatFits(CGSize(width: self.width - 70, height: msgHeight))
            self.msgLable.snp.remakeConstraints { (make) in
                make.top.equalTo(self.nameLB.snp.bottom).offset(10)
                make.left.right.equalTo(self.nameLB)
                make.bottom.greaterThanOrEqualToSuperview().offset(-5)
            }
        }
    }
    //类方法
    class func height(_ model: CommentModel?) -> CGFloat {
        var height: CGFloat = 65.0
        guard let model = model else {
            return height
        }
        let label = MYCoreTextLabel()
        label.textFont = UIFont.systemFont(ofSize: 13)
        label.lineSpacing = 2.0
        label.wordSpacing = 0.5
        label.setText(model.content_filter)
        height += label.sizeThatFits(CGSize(width: kSCREEN_WIDTH - 70, height: CGFloat.infinity)).height
        return height
    }
    
    fileprivate func stringHeight(_ desString: String, _ fontSize:CGFloat, _ maxWidth:CGFloat) -> CGFloat {
        let dic = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: fontSize)]
        let string = desString as NSString
        let height = string.boundingRect(with: CGSize(width:maxWidth,height:CGFloat(MAXFLOAT)), options: NSStringDrawingOptions(rawValue: NSStringDrawingOptions.RawValue(UInt8(NSStringDrawingOptions.usesLineFragmentOrigin.rawValue) | UInt8(NSStringDrawingOptions.usesFontLeading.rawValue))), attributes: dic, context: nil).size.height
        return height
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.addSubview(self.iconImgV)
        self.contentView.addSubview(self.nameLB)
        self.contentView.addSubview(self.msgLable)
        setUpSnap()
        selectionStyle = .none
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

///富文本代理
extension CommentCell : MYCoreTextLabelDelegate {
    func linkText(_ clickString: String!, type linkType: MYLinkType) {
        
    }
}




